package com.example.elasticsearchdemo;

import com.example.elasticsearchdemo.dao.EntrySearchDao;
import com.example.elasticsearchdemo.pojo.Entry;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

@SpringBootApplication
public class ElasticsearchDemoApplication {

    public static void main(String[] args) {
        try (ConfigurableApplicationContext context = SpringApplication.run(ElasticsearchDemoApplication.class, args)) {

            ElasticsearchTemplate template = context.getBean(ElasticsearchTemplate.class);
            EntrySearchDao searchDao = context.getBean(EntrySearchDao.class);

            searchDao.save(Entry.create("你好,我叫丁常磊", "我 是 丁常磊"));
            searchDao.save(Entry.create("edcba", "网易老总"));
            searchDao.save(Entry.create("丁明磊", ""));
            searchDao.save(Entry.create("丁光磊", "网易员工"));
            searchDao.save(Entry.create("光磊", ""));
            searchDao.save(Entry.create("丁宇", ""));

          /*  Page<Entry> entries = searchDao.queryInPage("丁磊",Sets.newHashSet("title","content"),1,10);


            entries.forEach(
                    it->{
                        System.out.println(it.getTitle());
                        System.out.println(it.getContent());
                    }
            );*/


           NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();

            builder.withQuery(QueryBuilders.termQuery("title","我是"));

            AggregatedPage<Entry> entries = template.queryForPage(builder.build(), Entry.class);

            entries.forEach(entry -> {System.out.println(entry.getTitle());});

            searchDao.deleteAll();

        }
    }}









/*
    启动elasticsearch镜像时要注意:
    1.宿主机执行  sudo sysctl -w vm.max_map_count=262144   以修改虚拟机参宿
    2.挂载本地配置文件 "elasticsearch.yml"
                    配置参数  "transport.host: 0.0.0.0"



    >. 一种简单的做法是使用集成好的镜像(无需以上配置)
         docker pull bachue/elasticsearch-ik:6.2.4

        该镜像继承了ik分词器,该分次器支持中文的分词,需要对字段使用@Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
        进行注解



 */